import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../config/color_config.dart';
import '../utils/common.dart';

class BottomDateTimeDialog extends StatefulWidget {
  final String? title;
  int? type = PickerDateTimeType.kYMD;
  final List<int>? customColumnType;
  final DateTimePickerAdapter? adapter;
  final DateTime? currentDate;
  final DateTime? maxDate;
  final DateTime? minDate;
  final Function(DateTime date)? confirm;
  bool? showTitle = true;
  bool? useSafeArea = true;
  bool? showUnit = true;
  final Widget? customSelectionOverlay;
  final double? pickerHeight;
  final Color? selectBgColor;

  BottomDateTimeDialog({
    Key? key,
    this.title,
    this.type,
    this.customColumnType,
    this.adapter,
    this.currentDate,
    this.maxDate,
    this.minDate,
    this.confirm,
    this.showTitle,
    this.useSafeArea,
    this.showUnit,
    this.customSelectionOverlay,
    this.pickerHeight,
    this.selectBgColor,
  }) : super(key: key);

  @override
  State<BottomDateTimeDialog> createState() => _DateTimeDialogState();
}

class _DateTimeDialogState extends State<BottomDateTimeDialog> {
  DateTimePickerAdapter? datePickerAdapter;
  final kToday = DateTime.now();

  @override
  void initState() {
    super.initState();
    datePickerAdapter ??= DateTimePickerAdapter(
      type: widget.type ?? PickerDateTimeType.kYMD,
      customColumnType: widget.customColumnType,
      value: widget.currentDate,
      maxValue: widget.maxDate ??
          DateTime(kToday.year + 10, kToday.month, kToday.day),
      minValue: widget.minDate ??
          DateTime(kToday.year - 10, kToday.month, kToday.day),
      isNumberMonth: true,
      yearSuffix: widget.showUnit! ? "年" : "",
      monthSuffix: widget.showUnit! ? "月" : "",
      daySuffix: widget.showUnit! ? "日" : "",
      hourSuffix: widget.showUnit! ? "时" : "",
      minuteSuffix: widget.showUnit! ? "分" : "",
      secondSuffix: widget.showUnit! ? "秒" : "",
    );
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(12.r),
                topRight: Radius.circular(12.r),
              ),
            ),
            child: _bodySty(context),
          ),
        ),
      ],
    );
  }

  _bodySty(BuildContext context) {
    if (widget.useSafeArea ?? true) {
      return SafeArea(
        top: false,
        child: _body(context),
      );
    } else {
      return _body(context);
    }
  }

  _body(BuildContext context) {
    return Column(
      children: [
        Visibility(
          visible: widget.showTitle ?? true,
          child: _title(context),
        ),
        _content()
      ],
    );
  }

  _title(BuildContext context) {
    return SizedBox(
      height: 54.w,
      child: Row(
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              Navigator.pop(context);
            },
            child: Container(
              height: double.infinity,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 15.w),
              child: Text(
                "取消",
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: ColorConfig.FF666666,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ),
          Expanded(
            child: Text(
              widget.title ?? "",
              textAlign: TextAlign.center,
              style: TextStyle(
                color: ColorConfig.FF333333,
                fontSize: 16.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          GestureDetector(
            onTap: () {
              Navigator.pop(context);
              widget.confirm?.call(DateTime.parse(datePickerAdapter!.text));
            },
            child: Container(
              height: double.infinity,
              alignment: Alignment.center,
              color: Colors.white,
              padding: EdgeInsets.symmetric(horizontal: 14.w),
              child: Text(
                "确定",
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: ColorConfig.main,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  _content() {
    return Container(
      child: Picker(
          hideHeader: true,
          height: widget.pickerHeight ?? 150,
          itemExtent: 40.w,
          selectionOverlay:
              widget.customSelectionOverlay ?? _defaultSelectionOverlay(),
          textStyle: TextStyle(
              color: ColorConfig.FF020306,
              fontSize: 16.sp,
              fontWeight: FontWeight.w400),
          selectedTextStyle: TextStyle(
            color: ColorConfig.FF333333,
            fontSize: 21.sp,
          ),
          adapter: datePickerAdapter!,
          title: Text(widget.title ?? ""),
          onConfirm: (Picker picker, List value) {
            logger.i((picker.adapter as DateTimePickerAdapter).value);
          }).makePicker(),
    );
  }

  _defaultSelectionOverlay() {
    return Container(
      // margin: EdgeInsets.symmetric(horizontal: 8.w),
      decoration: BoxDecoration(
        color: widget.selectBgColor ?? ColorConfig.black.withOpacity(0.05),
        // borderRadius: BorderRadius.circular(10.r),
      ),
    );
  }
}

void showBottomDateTimeDialog(
  BuildContext context, {
  String? title,
  int? type = PickerDateTimeType.kYMD,
  List<int>? customColumnType,
  DateTimePickerAdapter? adapter,
  DateTime? currentDate,
  DateTime? maxDate,
  DateTime? minDate,
  Function(DateTime date)? confirm,
  bool? showTitle = true,
  bool? useSafeArea = true,
  bool? showUnit = true,
  Widget? customSelectionOverlay,
  double? pickerHeight,
  Color? selectBgColor,
}) {
  showModalBottomSheet(
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      context: context,
      builder: (BuildContext context) {
        return SizedBox(
          height: ScreenUtil().bottomBarHeight+255.w,
          child: BottomDateTimeDialog(
              confirm: confirm,
              title: title,
              adapter: adapter,
              type: type,
              customColumnType: customColumnType,
              currentDate: currentDate,
              maxDate: maxDate,
              minDate: minDate,
              showTitle: showTitle,
              showUnit: showUnit,
              useSafeArea: useSafeArea,
              customSelectionOverlay: customSelectionOverlay,
              selectBgColor: selectBgColor),
        );
      });
}
